package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsClient;
import com.yidu.ruifeng_logistics.service.LogisticsClientService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsClient)表控制层
 *
 * @author makejava
 * @since 2021-03-09 14:24:37
 */
@Api(description = "瑞丰物流寄件人客户接口")
@RestController
@RequestMapping("logisticsClient")
public class LogisticsClientController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsClientService logisticsClientService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据ID查询单条信息", notes = "作为修改数据回显使用")
    @PostMapping("selectOne")
    public BaseMessage selectOne(Integer id) {
        LogisticsClient logisticsClient = logisticsClientService.queryById(id);
        return BaseMessage.success(logisticsClient);
    }

    /*
     分页查询
     */
    @ApiOperation(value = "分页显示用户", notes = "分页返回所有用户信息")
    @GetMapping("findAll")
    public Map<String, Object> selectAll(LogisticsClient logisticsClient, int page, int limit) {

        PageHelper.startPage(page, limit);
        List<LogisticsClient> logisticsClients = logisticsClientService.queryAll(logisticsClient);
        //  查询到集合之后，将集合数据封装到分页助手的PageInfo中
        PageInfo<LogisticsClient> info = new PageInfo<>(logisticsClients);
        Map<String, Object> map = new HashMap();
        map.put("msg", "");
        map.put("count", info.getTotal());
        map.put("data", logisticsClients);
        map.put("code", 0);
        return map;
    }

    /*
 分页查询
 */
    @ApiOperation(value = "根据ID删除", notes = "删除单个用户")
    @PostMapping("deleteById")
    public BaseMessage deleteById(String id) {
        boolean b = logisticsClientService.deleteById(Integer.valueOf(id));
        return b == true ? BaseMessage.success() : BaseMessage.fail();
    }

    /**
     * 修改或增加
     *
     * @param logisticsClient
     * @return
     */
    @ApiOperation(value = "根据ID判断修改还是增加", notes = "有ID修改 无ID增加")
    @PostMapping("updateOrAdd")
    public BaseMessage updateOrAdd(LogisticsClient logisticsClient) {
        BaseMessage baseMessage = new BaseMessage();
        if (logisticsClient.getClientId() == null) {
            logisticsClientService.insert(logisticsClient);
            baseMessage.setMsg("增加成功");
            baseMessage.setState(1);
        } else {
            logisticsClientService.update(logisticsClient);
            baseMessage.setMsg("修改成功");
            baseMessage.setState(1);
        }
        return baseMessage;
    }

    @ApiOperation(value = "根据ID删除多个", notes = "删除多个")
    @PostMapping("deleteAllId")
    public BaseMessage deleteAllId(String id) {
        String sid = id.substring(0, id.length() - 1);
        System.out.println("截取之后的 " + sid);
        String[] split =sid.split(",");
        int i = logisticsClientService.deleteAllId(split);
        return i>0?BaseMessage.success():BaseMessage.success();
    }


}
